home *** CD-ROM | disk | FTP | other *** search
/ Game Cracker (Expanded Edition) / Game Cracker (Expanded Edition).iso / cracks / Q2HDFIX.ZIP / PATCH.CPP next >
C/C++ Source or Header  |  1997-12-16  |  6KB  |  173 lines

  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <sys\stat.h>
  4. #include <io.h>
  5. #include <fcntl.h>
  6.  
  7. /* CHANGE
  8. 0042B1CC 68 A8 47 44 00       push        4447A8h
  9. 0042B1D1 52                   push        edx
  10. 0042B1D2 E8 D9 25 00 00       call        0042D7B0
  11. 0042B1D7 83 C4 08             add         esp,8
  12. 0042B1DA 85 C0                test        eax,eax
  13. 0042B1DC 74 15                je          0042B1F3
  14. 0042B1DE 50                   push        eax
  15. 0042B1DF E8 AC 20 00 00       call        0042D290
  16. 0042B1E4 83 C4 04             add         esp,4
  17. 0042B1E7 8D 44 24 04          lea         eax,dword ptr [esp+4]
  18. 0042B1EB 50                   push        eax
  19. 0042B1EC FF D6                call        esi
  20. 0042B1EE 83 F8 05             cmp         eax,5
  21. 0042B1F1 74 63                je          0042B256
  22. */
  23.  
  24. /* TO
  25. 0042B1CC 90 90 90 90 90       nop nop nop nop nop
  26. 0042B1D1 90                   nop
  27. 0042B1D2 90 90 90 90 90       nop nop nop nop nop
  28. 0042B1D7 90 90 90             nop nop nop
  29. 0042B1DA 90 90                nop nop
  30. 0042B1DC 90 90                nop nop
  31. 0042B1DE 90                   nop
  32. 0042B1DF 90 90 90 90 90       nop nop nop nop nop
  33. 0042B1E4 90 90 90             nop nop nop
  34. 0042B1E7 90 90 90 90          nop nop nop nop
  35. 0042B1EB 90                   nop
  36. 0042B1EC 90 90                nop nop
  37. 0042B1EE 33 C0                xor         eax,eax
  38. 0042B1F0 90                   nop
  39. 0042B1F1 74 63                je          0042B256
  40. */
  41.  
  42. // Note: 0xff means any value is acceptable. Handles executable
  43. // relocation differences between versions.
  44. BYTE abSearch[] = { 
  45.   0x68, 0xff, 0xff, 0xff, 0xff,
  46.   0x52, 
  47.   0xe8, 0xff, 0xff, 0xff, 0xff,
  48.   0x83, 0xc4, 0x08,
  49.   0x85, 0xc0,
  50.   0x74, 0x15,
  51.   0x50,
  52.   0xe8, 0xff, 0xff, 0xff, 0xff,
  53.   0x83, 0xc4, 0x04,
  54. };
  55. const int iSearchLen = sizeof(abSearch) / sizeof(abSearch[0]);
  56.  
  57. BYTE abReplace[] = {
  58.   0x90, 0x90, 0x90, 0x90, 0x90,
  59.   0x90,
  60.   0x90, 0x90, 0x90, 0x90, 0x90,
  61.   0x90, 0x90, 0x90,
  62.   0x90, 0x90,
  63.   0x90, 0x90,
  64.   0x90,
  65.   0x90, 0x90, 0x90, 0x90, 0x90,
  66.   0x90, 0x90, 0x90,
  67.   0x90, 0x90, 0x90, 0x90,
  68.   0x90,
  69.   0x90, 0x90,
  70.   0x33, 0xc0,
  71.   0x90
  72. };
  73. const int iReplaceLen = sizeof(abReplace) / sizeof(abReplace[0]);
  74.  
  75. const char szExecutable[] = "quake2.exe";
  76.  
  77. int
  78. main(void)
  79. {
  80.   printf("** NOT FOR USE WITH UNLICENCED SOFTWARE **\n\n");
  81.   printf("This program patches the Quake2(tm)(r) executable so that it\n");
  82.   printf("can be run from a hard disk without the CD present.\n\n");
  83.   printf("Run this program in the same directory as quake2.exe\n");
  84.   printf("to generate quake2.exe.patched\n\n");
  85.  
  86.   struct stat fileInfo;
  87.   if (stat(szExecutable, &fileInfo) == 0)
  88.     {
  89.       const int iFileSize = fileInfo.st_size;
  90.       PBYTE pBytes = new BYTE[iFileSize];
  91.       if (pBytes)
  92.         {
  93.           int fdFile = open(szExecutable, O_RDONLY | O_BINARY);
  94.           if (fdFile != -1)
  95.             {
  96.               int iRead = read(fdFile, pBytes, iFileSize);
  97.               if (iRead == iFileSize)
  98.                 {
  99.                   printf("Looking for the following byte sequence:\n  ");
  100.                   for (int iSeq = 0; iSeq < iSearchLen; iSeq++)
  101.                     {
  102.                       if (iSeq && !(iSeq & 7))
  103.                         printf("\n  ");
  104.                       if (abSearch[iSeq] == 0xff)
  105.                         printf("-- ", abSearch[iSeq]);
  106.                       else
  107.                         printf("%02x ", abSearch[iSeq]);
  108.                     }
  109.                   printf("\n\n");
  110.  
  111.                   BOOL bFound = FALSE;
  112.  
  113.                   for (PBYTE pByte = pBytes;
  114.                        pByte < pBytes + iFileSize - iSearchLen;
  115.                        pByte++)
  116.                     {
  117.                       for (int iSeq = 0; iSeq < iSearchLen; iSeq++)
  118.                         if (abSearch[iSeq] != 0xff &&
  119.                             pByte[iSeq] != abSearch[iSeq])
  120.                           goto notFound;
  121.  
  122.                       printf("FOUND at offset 0x%08x\n", pByte - pBytes);
  123.                       if (bFound)
  124.                         printf("WARNING: byte sequence found more than once.\n");
  125.  
  126.                       for (iSeq = 0; iSeq < iReplaceLen; iSeq++)
  127.                         pByte[iSeq] = abReplace[iSeq];
  128.                       bFound = TRUE;
  129.                                               
  130.                       notFound: ;
  131.                     }
  132.  
  133.                   if (bFound)
  134.                     {
  135.                       char szPatched[sizeof(szExecutable) + 32];
  136.                       strcpy(szPatched, szExecutable);
  137.                       strcat(szPatched, ".patched");
  138.  
  139.                       printf("Executable patched, writing new executable to: %s\n", szPatched);
  140.  
  141.                       int fdFile = open(szPatched, O_WRONLY | O_BINARY | O_CREAT, S_IREAD | S_IWRITE);
  142.                       if (fdFile != -1)
  143.                         {
  144.                           int iWritten = write(fdFile, pBytes, iFileSize);
  145.                           if (iWritten != iFileSize)
  146.                             printf("Can't write data to %s : %s\n", szPatched, strerror(errno));
  147.                           close(fdFile);
  148.                         }
  149.                       else
  150.                         printf("Can't create %s : %s\n", szPatched, strerror(errno)); 
  151.                     }
  152.                   else
  153.                     printf("Couldn't find required byte sequence to patch it\n");
  154.                 }
  155.               else
  156.                 printf("Can't read %s : %s\n", szExecutable, strerror(errno));
  157.  
  158.               close(fdFile);
  159.             }
  160.           else
  161.             printf("Can't open %s, %s\n", szExecutable, strerror(errno));
  162.            
  163.           delete [] pBytes;
  164.         }
  165.       else
  166.         printf("Can't allocate memory to hold %s\n", szExecutable);
  167.     }
  168.   else
  169.     printf("Can't open %s : %s\n", szExecutable, strerror(errno));
  170.  
  171.   return 0;
  172. }
  173.